﻿using Microsoft.AspNetCore.Mvc;
using My.FrameWork.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace My.WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class HomeController : Controller
    {
         public  MyContext Con { get; }
        // private readonly MyContext Con;


        public HomeController(MyContext con) 
        {
            Con = con;
        }



        public JsonResult Index()
        {

            var result=  Con.DV_B_PermissionSettings.Where(x => x.Browers == 1).ToList();
            return Json(new{ code=1,data= result, msg="success"});
              
        }

        /// <summary>
        /// 关联查询
        /// </summary>
        /// <returns></returns>
        public JsonResult RelevanceSelect()
        {
            var result=  Con.DV_B_PermissionMenus.Join(Con.DV_B_PermissionSettings, x => x.PermissionMenuID, z => z.PermissionMenuID ,(x,z)=>new {x.PermissionMenuID,x.MenuName,z.RoleName,z.Browers }).Where(x=>x.Browers==1).ToList();
            return Json(new { code = 1, data = result, msg = "success" });

        }

        public string Insert() 
        {

            DV_B_PermissionMenu menu = new DV_B_PermissionMenu();

            Con.DV_B_PermissionMenus.Add(new DV_B_PermissionMenu()
            {
                PermissionMenuID = "1"

            });
            Con.SaveChanges();
            return "1";
        }


        public JsonResult Update()
        {
            DV_B_PermissionMenu menu = new DV_B_PermissionMenu();
            menu.PermissionMenuID = "1";

            var result = Con.DV_B_PermissionMenus.Where(menu => menu.PermissionMenuID == "1").FirstOrDefault();
            result.MenuUrl = "测试";
            int resultCode=  Con.SaveChanges();

            return Json(new { code = 1, data = result, msg = "success" });
        }

        public JsonResult UpdateMultiple()
        {
            DV_B_PermissionMenu menu = new DV_B_PermissionMenu();
            menu.PermissionMenuID = "1";

            var result = Con.DV_B_PermissionMenus.Where(menu => menu.MenuType =="1").ToList();
            foreach (var s in result) 
            {
                s.MenuIcon = "菜单" + result.IndexOf(s);
            }
         
            int resultCode = Con.SaveChanges();

            return Json(new { code = 1, data = result, msg = "success" });
        }



        public JsonResult Delete()
        {
            DV_B_PermissionMenu menu = new DV_B_PermissionMenu();
            menu.PermissionMenuID = "1";
            var result = Con.DV_B_PermissionMenus.Where(menu => menu.PermissionMenuID == "1").FirstOrDefault();
            Con.DV_B_PermissionMenus.Remove(result);
            int resultCode = Con.SaveChanges();

            return Json(new { code = 1, data = "", msg = "success" });
        }

        //多个删除一样
    }
}
